.PHONY: run qemu build

KERNEL = target/riscv64gc-unknown-none-elf/release/rust_os
KERNEL_BIN = /root/rust_os/target/riscv64gc-unknown-none-elf/release/rust_os.bin
BOOTLOADER_QEMU = bootloader/rustsbi-qemu.bin
BOOTLOADER_K210 = bootloader/rustsbi-qemu.bin

CLEAN = cargo clean
BUILD = cargo build --release
STRIP = rust-objcopy --strip-all $(KERNEL) -O binary $(KERNEL_BIN)
QEMU = qemu-system-riscv64 \
              -machine virt \
              -nographic \
              -bios $(BOOTLOADER_QEMU) \
              -device loader,file=$(KERNEL_BIN),addr=0x80200000

build:
	$(BUILD)

base:
	$(CLEAN)
	$(BUILD)
	$(STRIP)

qemu:
	make base
	$(QEMU)

k210:
	make base
	cp $(BOOTLOADER_K210) $(BOOTLOADER_K210).copy
	dd if=$(KERNEL_BIN) of=$(BOOTLOADER_K210).copy bs=128K seek=1
	mv $(BOOTLOADER_K210).copy $(KERNEL_BIN)

qemu-gdb:
	$(CLEAN)
	$(BUILD)
	$(STRIP)
	$(QEMU) -s -S

gdbv:
	gdb-multiarch target/riscv64gc-unknown-none-elf/release/rust_os

cp:
	docker cp my_rust:$(KERNEL_BIN) .